<%@ page language="java" import="java.util.*,com.css.vo.*" pageEncoding="UTF-8"%>
<%@ page import="com.css.dao.*,com.css.dao.impl.*"%>
<%@ page import="com.css.dao.*,com.css.action.*"%>
<%@ page import="com.css.dao.*,com.css.service.*"%>
<%@ page import="org.springframework.context.*,org.springframework.context.support.*"%>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>销售信息管理</title>
  <link rel="stylesheet" href="./../styles.css">
</head>
<body>
  <header>
  	<h1>销售信息管理</h1>
    <nav>
      <ul> 
      	<li><a href="./../index.jsp">主页</a></li>
        <li><a href="getList.action">系统管理</a></li>
        <li><a href="../Customer/customer-management.jsp">客户信息管理</a></li>
        <li><a href="../Car/car-management.jsp">汽车信息管理</a></li>
        <li><a href="../Employee/employee-management.jsp">员工信息管理</a></li>
        <li><a href="../Sale/sales-management.jsp">销售信息管理</a></li>
      </ul>
    </nav>
  </header>

   <div class="content">
    <h2>销售信息列表</h2>

	<form action="searchSale.action" method="post">
      <label for="search">搜索：</label>
      <input type="text" id="search" name="search" placeholder="请输入销售ID、客户、员工或汽车型号" />
      <button type="submit">搜索</button>
    </form>
    
    <button onclick="location.href='add-sale.jsp'">新增销售订单</button>
    
    <table>
      <thead>
        <tr>
          <th>销售ID</th>
          <th>汽车型号</th>
          <th>客户</th>
          <th>员工</th>
          <th>销售价格</th>
          <th>销售日期</th>
          <th>交付日期</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <% 
          // 获取销售数据
          ApplicationContext myaApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
  		  ISaleService saleService = (ISaleService)myaApplicationContext.getBean("saleService");
  		  ICarService carService = (ICarService)myaApplicationContext.getBean("carService");
  		  ICustomerService customerService = (ICustomerService)myaApplicationContext.getBean("customerService");
  		  IEmployeeService employeeService = (IEmployeeService)myaApplicationContext.getBean("employeeService");
          List<Sale> saleList = saleService.findAllSale();
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          if (saleList != null && !saleList.isEmpty()) {
              for (Sale sale : saleList) {
              Car car = carService.findById(sale.getCarId());
              Customer customer = customerService.findById(sale.getCustomerId());
              Employee employee = employeeService.findById(sale.getEmployeeId());
        %>
        <tr>
          <td><%= sale.getSaleId() %></td>
          <td><%= car.getCarModel() + car.getVersion() + car.getYear() + "款" + car.getColor() %></td>
          <td><%= customer.getCustomerName() %></td>
          <td><%= employee.getEmployeeName() %></td>
          <td><%= sale.getSaleAmount() %></td>
          <td><%= sdf.format(sale.getOrderDate()) %></td>
          <td><%= sdf.format(sale.getDeliveryDate()) %></td>          
          <td>
          	<form action="updateSaleView.action" method="post">
    			<input type="hidden" name="saleId" value="<%= sale.getSaleId() %>">
    			<button type="submit">修改</button>
  			</form>
            <form action="deleteSale.action" method="post" onsubmit="return confirm('确定删除销售信息吗?');">
    			<input type="hidden" name="saleId" value="<%= sale.getSaleId() %>">
    			<button type="submit">删除</button>
  			</form>
          </td>
        </tr>
        <% 
              }
          } else {
        %>
        <tr>
          <td colspan="9" style="text-align: center;">暂无数据</td>
        </tr>
        <% } %>
      </tbody>
    </table>
    
	<table width="450" align="center" border="0">
	<!-- 分页 -->

    </table>

  </div>
</body>
</html>
